<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../css/public.css">
</head>
<body>
    <!-- strokeStyle 设置或返回用于笔触的颜色、渐变或模式-->
    <!-- getContext() 返回一个用于在画布上绘图的环境-->
    <!-- strokeRect(x,y,width,height) 绘制矩形（无填充）-->
    <!-- createLinearGradient(x0,y0,x1,y1) 创建线性渐变（用在画布内容上）-->
    <!-- addColorStop(stop,color) 规定渐变对象中的颜色和停止位置-->

    <!--纯色-->
    <canvas id="myCanvas1" width="300" height="150" style="border:1px solid #d3d3d3;"></canvas>
    <!--渐变-->
    <canvas id="myCanvas2" width="300" height="150" style="border:1px solid #d3d3d3;"></canvas>


    <script>
        // 纯色
        var a = document.getElementById('myCanvas1');
        var atx = a.getContext('2d');
        atx.strokeStyle = "#ffac20";
        atx.strokeRect(20,20,150,100);

        //渐变
        var b = document.getElementById('myCanvas2');
        var btx = b.getContext('2d');
        // var b_gradient = btx.createLinearGradient(0,0,0,170);//纵向
        var b_gradient = btx.createLinearGradient(0,0,170,0);//横向
        b_gradient.addColorStop(0,"red");
        b_gradient.addColorStop(1,"black");
        btx.strokeStyle = b_gradient;
        btx.strokeRect(20,20,150,100);
    </script>
</body>
</html>